import { UserPosVo } from "./../../../model/UserPosVo";
import { UserService } from "./../../../providers/UserService";
import { HomePage } from "./../../home/home";
import { Component, Injector } from "@angular/core";
import {
  IonicPage,
  NavController,
  NavParams,
  ActionSheetController
} from "ionic-angular";
import { BasePage } from "../../basePage";
import { StorageService } from "./../../../providers/StorageService";
import { nailerService } from "./../../../providers/nailerService";
import { PIC_URL } from "./../../../providers/Config";

/**
 * Generated class for the StoreShowPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: "page-store-show",
  templateUrl: "store-show.html"
})
export class StoreShowPage extends BasePage {
  picrooturl = ""; //图片服务地址
  userinfo = {};
  collectionlist = [];
  activation = false; //激活图片删除
  flag1: boolean;
  flag2: boolean;
  search: boolean;
  UpoutShow: boolean = false;
  data = []; //搜索要放的数组
  selectallflag = false; //全选全不选
  Upout = "全部";
  Ulli = ["全部", "上传款式", "已买款式", "美甲师款式", "未上架"];
  Li_index: number;
  searchObj = {
    user_id: 0,
    collect_type: 1
  };
  is_buy = 0;
  Thide = 0;
  myshop = {
    shop_id: "1114"
  };
  user_info: any;
  on_sale = 0;
  /*上下架参数*/
  suno = {
    service_id: "58" /*美甲服务ID*/,
    // undercarriage_type: 0, //1 下架美甲款式 0 下架美甲服务
    // nail_id: "", //美甲款式 id
    operate_type: 0, //1 上架 0 下架
    user_id: ""
  };
  content: "";
  shopbuff = "";

  constructor(
    injector: Injector,
    public nailerservice: nailerService,
    public actionSheetCtrl: ActionSheetController,
    public stc: StorageService,
    public NavServer: UserService,
    public storage: StorageService
  ) {
    super(injector);
    this.picrooturl = PIC_URL;
    this.userinfo = this.stc.read("userinfo");
  }
  // User_id = "";
  ionViewDidLoad() {
    console.log("ionViewDidLoad CollectionPage");
    this.getUserCollectionAPI();
    console.log(this.collectionlist);
    // this.suno.user_id=this.userinfo['user_id']
    // console.log()
  }

  /**点击更多的时候 */
  moreCLI(i, j, item) {
    console.log(i);
    let actionSheet = this.actionSheetCtrl.create({
      buttons: [
        {
          text: "编辑",
          handler: () => {
            // this.collectionlist[i]['data'][j]['oper']=3;
            // console.log(this.collectionlist[i]['data'][j]);
            // this.navCtrl.push("IssueImgPage", { type: 3, detailObj: item });
            let modal=this.modalCtrl.create("IssueImgPage", { type: 3, detailObj: item });
            modal.onDidDismiss(data=>{
              this.getUserCollectionAPI();
            });
            modal.present();
          }
        },
        {
          text: "删除",
          handler: () => {
            let alert = this.alertCtrl.create({
              title: "删除",
              message: "真的要删除吗?",
              buttons: [
                {
                  text: "取消",
                  role: "cancel",
                  handler: () => {
                    console.log("Cancel clicked");
                  }
                },
                {
                  text: "确认删除",
                  handler: () => {
                    let collected_id = this.collectionlist[i]["data"][j][
                      "service_id"
                    ];
                    this.editShopServiceAPI(collected_id);
                  }
                }
              ]
            });
            alert.present();
          }
        },

        {
          text: "取消",
          role: "cancel",
          handler: () => {
            console.log("Cancel clicked");
          }
        }
      ]
    });
    actionSheet.present();
  }

  /*上架服务*/
  presentToast() {
    let toast = this.toastCtrl.create({
      message: this.shopbuff,
      duration: 1000
    });
    toast.present();
  }
  async Upshop(i, j) {
    console.log(this.collectionlist[i]["data"][j]);
    this.suno.user_id = this.userinfo["user_id"];
    let id = localStorage.userVo;
    this.shopbuff = "上架成功";
    this.suno.service_id = this.collectionlist[i]["data"][j].service_id;
    this.suno.operate_type = 1;
    let res = await this.NavServer.operateShelfAPI(this.suno);
    console.log(this.suno);
    console.log(res);
    this.Upout = "全部";
    this.Li_index = 0;
    this.getUserCollectionAPI();
    this.presentToast();
  }
  /*下架服务*/

  async Outshop(i, j) {
    this.suno.service_id = this.collectionlist[i]["data"][j].service_id; //服务ID
    this.suno.operate_type = 0;
    this.shopbuff = "下架成功";
    this.suno.user_id = this.userinfo["user_id"];
    let res = await this.NavServer.operateShelfAPI(this.suno);
    console.log(this.suno);
    console.log(res);
    this.Upout = "全部";
    this.Li_index = 0;
    this.getUserCollectionAPI();
    this.presentToast();
  }
  dismissCLI() {
    this.viewCtrl.dismiss();
  }
  /*下拉菜单*/
  GoUlli() {
    if (this.UpoutShow == true) {
      this.UpoutShow = false;
    } else {
      this.UpoutShow = true;
    }
  }

  /*筛选列表*/

  async ShowVal(i) {
    this.Li_index = i;
    this.collectionlist = [];
    this.Upout = this.Ulli[i];
    // console.log(this.Ulli[i]);
    // console.log(i);
    let start = setTimeout(() => {
      this.UpoutShow = false;
    }, 300);
    this.Thide = i;
    let param = { user_id: this.userinfo["user_id"], show_type: 1 };
    let loader = this.loadingCtrl.create({
      content: ""
    });
    loader.present();
    let result = await this.nailerservice.showShopManageAPI(param);
    loader.dismiss();
    if (result.code != 1) {
      let toast = this.toastCtrl.create({
        message: result.msg,
        duration: 3000,
        position: "top"
      });
      toast.present();
    } else {
      this.collectionlist = result.data;

      // if (this.collectionlist.length > 0) {
      for (let index = 0; index < this.collectionlist.length; index++) {
        let dele = this.collectionlist[index]["data"];
        for (let j = 0; j < this.collectionlist[index]["data"].length; j++) {
          this.on_sale = this.collectionlist[index]["data"][j]["on_sale"];
          this.is_buy = this.collectionlist[index]["data"][j]["is_buy"];
          //  console.log(index)
          /*全部*/

          if (this.Li_index == 0) {
            this.collectionlist = result.data;
          }
          /*上传款式*/

          if (this.Li_index == 1) {
            if (this.is_buy != 0) {
              dele.splice(j, 1);
              j = j - 1;
            }
          }
          /*购买款式*/
          if (this.Li_index == 2) {
            if (this.is_buy != 1) {
              dele.splice(j, 1);
              j = j - 1;
            }
          }
          /**美甲师款式 */
          if (this.Li_index == 3) {
            if (this.is_buy != 2) {
              dele.splice(j, 1);
              j = j - 1;
            }
          }
          /**未上架 */
          if (this.Li_index == 4) {
            if (this.on_sale == 1) {
              dele.splice(j, 1);
              j = j - 1;
            }
          }
        }
      }
    }
  }
  // }

  /**启用删除小图标 */
  activationCLI() {
    this.activation = true;
  }

  /**完成启用图标 */
  activationokCLI() {
    this.activation = false;
  }

  /**删除店铺秀 批量删除 */
  async delallNailAPI() {
    let collected_ids = "";
    for (let i = 0; i < this.collectionlist.length; i++) {
      for (let j = 0; j < this.collectionlist[i]["data"].length; j++) {
        if (this.collectionlist[i]["data"][j]["select"]) {
          collected_ids +=
            this.collectionlist[i]["data"][j]["service_id"] + ",";
        }
      }
    }
    collected_ids = collected_ids.substring(0, collected_ids.length - 1);
    this.editShopServiceAPI(collected_ids);
  }

  /**删除具体的逻辑代码 */
  async editShopServiceAPI(collected_ids) {
    let param = { type: 0, service_ids: collected_ids };
    let result = await this.nailerservice.editShopServiceAPI(param);
    console.log(result);
    if (result.code != 1) {
      let toast = this.toastCtrl.create({
        message: result.msg,
        duration: 3000,
        position: "top"
      });
      toast.present();
    } else {
      let toast = this.toastCtrl.create({
        message: result.msg,
        duration: 2000,
        position: "top"
      });
      toast.present();
      this.getUserCollectionAPI();
    }
  }
  // async getShopBuyNailListAPI(){
  //   console.log(this.myshop.shop_id)
  //   let res=await this.nailerservice.getShopBuyNailListAPI(this.myshop)
  //   console.log(res)
  // }
  /**删除实现的具体逻辑 */

  /**全选 */
  selectall() {
    if (!this.selectallflag) {
      if (this.collectionlist.length > 0) {
        for (let i = 0; i < this.collectionlist.length; i++) {
          for (let j = 0; j < this.collectionlist[i]["data"].length; j++) {
            this.collectionlist[i]["data"][j]["select"] = true;
            this.collectionlist[i]["data"][j]["selectimg"] =
              "assets/img/selectok.png";
          }
        }
      }
      this.selectallflag = true;
    } else {
      if (this.collectionlist.length > 0) {
        for (let i = 0; i < this.collectionlist.length; i++) {
          for (let j = 0; j < this.collectionlist[i]["data"].length; j++) {
            this.collectionlist[i]["data"][j]["select"] = false;
            this.collectionlist[i]["data"][j]["selectimg"] =
              "assets/img/selectno1.png";
          }
        }
      }
      this.selectallflag = false;
    }
  }

  /**选择删除 */
  selectCLI(i, j) {
    if (this.collectionlist[i]["data"][j]["select"]) {
      this.collectionlist[i]["data"][j]["select"] = false;
      this.collectionlist[i]["data"][j]["selectimg"] =
        "assets/img/selectno1.png";
      this.selectallflag = false;
    } else {
      this.collectionlist[i]["data"][j]["select"] = true;
      this.collectionlist[i]["data"][j]["selectimg"] =
        "assets/img/selectok.png";
    }
  }

  /**跳转详情页面 */
  detaligoodCLI(i, j) {
    console.log("我是详情");
  }

  /**请求后台的收藏列表 */
  async getUserCollectionAPI() {
    let param = { user_id: this.userinfo["user_id"], show_type: 1 };
    let loader = this.loadingCtrl.create({
      content: ""
    });
    loader.present();
    let result = await this.nailerservice.showShopManageAPI(param);
    loader.dismiss();
    if (result.code != 1) {
      let toast = this.toastCtrl.create({
        message: result.msg,
        duration: 3000,
        position: "top"
      });
      toast.present();
    } else {
      this.collectionlist = result.data;
      console.log(this.collectionlist);
      if (this.collectionlist.length > 0) {
        for (let i = 0; i < this.collectionlist.length; i++) {
          for (let j = 0; j < this.collectionlist[i]["data"].length; j++) {
            this.collectionlist[i]["data"][j]["select"] = false;
            this.collectionlist[i]["data"][j]["selectimg"] =
              "assets/img/selectno1.png";
            // console.log(this.collectionlist[i]["data"][j]["on_sale"]);
            this.on_sale = this.collectionlist[i]["data"][j]["on_sale"];
            // console.log(this.on_sale);
          }
        }
      }
    }
  }

  /** 添加服务*/
  IssueImgadd() {
    // this.navCtrl.push('IssueImgPage');
    let modal = this.modalCtrl.create("IssueImgPage", { type: 1 });
    modal.onDidDismiss(data => {});
    modal.present();
  }

  /*点击更新*/
  reload() {
    location.reload();
  }
  /*去首页*/
  home() {
    this.navCtrl.push("home");
  }
  searchby() {
    this.search = true;
  }

  async change() {
    if (this.content == "") {
      this.getUserCollectionAPI();
    } else {
      console.log(this.userinfo);
      let param = {
        shop_id: this.userinfo["shop_id"],
        service_name: this.content
      };
      let result = await this.nailerservice.searchShopServiceAPI(param);
      console.log(result);
      //因为原数据里是二维数组，所以为了方便，把搜索出来的数据放在data里
      //再把data放进要遍历的数组里，强行造一个新的二维数组
      this.collectionlist = [];
      this.collectionlist.push(this.data);
      console.log(this.collectionlist);
      this.collectionlist[0]["data"] = result.data;
      console.log(this.collectionlist);
    }

    // if(this.content==undefined||this.content.length==0){
    //   this.search=false;
    // }
    // console.log(this.content);
  }
  back() {
    if (this.content == undefined || this.content == "") {
      this.search = false;
    }
    console.log(this.content);
  }
}
